package com.gnerv.business.platform.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.gnerv.business.platform.common.response.R;
import com.gnerv.business.platform.common.util.UUIDUtil;
import com.gnerv.business.platform.entity.Dept;
import com.gnerv.business.platform.service.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description: 部门管理前端控制器
 * @author: Gnerv LiGen
 * @date: 2018-07-05
 **/
@Api(tags = "部门管理")
@RestController
@RequestMapping("/platform/dept")
public class DeptController implements BaseController<Dept> {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DeptService deptService;

    @ApiOperation(value = "创建部门", notes = "创建部门")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "deptName", value = "部门名称", required = true, paramType = "query"),
        @ApiImplicitParam(name = "pId", value = "上级部门ID", required = true, paramType = "query")
    })
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @Override
    public R create(Dept dept) {
        boolean checkDeptName = deptService.checkDeptName(dept);
        if (checkDeptName) {
            return R.error("500", "存在相同名称的同级部门!");
        }
        dept.setDeptId(UUIDUtil.getLowerUUID());
        boolean b = deptService.insert(dept);
        return b ? R.ok(R.OK_200) : R.error(R.ERROR_500);
    }

    @ApiOperation(value = "删除部门", notes = "根据部门ID 删除部门")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "deptId", value = "部门ID", required = true, paramType = "path")
    })
    @RequestMapping(value = "/delete/{deptId}", method = RequestMethod.GET)
    @Override
    public R delete(@PathVariable(value = "deptId") String deptId) {
        boolean checkHasChlidDept = deptService.checkHasChlidDept(deptId);
        if (checkHasChlidDept) {
            return R.error("500", "存在下级部门，需要删除下级部门!");
        }
        Dept dept = new Dept();
        dept.setDeptId(deptId);
        boolean b = deptService.delete(new EntityWrapper<>(dept));
        return b ? R.ok(R.OK_200) : R.error(R.ERROR_500);
    }

    @ApiOperation(value = "更新部门信息", notes = "根据部门ID 更新部门属性值")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "deptId", value = "部门ID", required = true, paramType = "query"),
        @ApiImplicitParam(name = "pId", value = "上级部门ID", required = true, paramType = "query"),
        @ApiImplicitParam(name = "deptName", value = "部门名称", required = true, paramType = "query")
    })
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @Override
    public R update(Dept dept) {
        boolean b = deptService.updateByDeptId(dept);
        return b ? R.ok(R.OK_200) : R.error(R.ERROR_500);
    }

    @ApiOperation(value = "查询单个部门", notes = "根据部门ID 查询单个部门")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "deptId", value = "部门ID", required = true, paramType = "path")
    })
    @RequestMapping(value = "/select/{deptId}", method = RequestMethod.GET)
    @Override
    public R select(@PathVariable(value = "deptId") String deptId) {
        Dept dept = new Dept();
        dept.setDeptId(deptId);
        dept = deptService.selectOne(new EntityWrapper<>(dept));
        return dept != null ? R.ok(R.OK_200).data("dept", dept) : R.error(R.ERROR_500);
    }

    @ApiOperation(value = "查询部门列表", notes = "根据条件 查询部门列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "deptId", value = "部门ID", paramType = "query"),
        @ApiImplicitParam(name = "pId", value = "上级部门ID", paramType = "query"),
        @ApiImplicitParam(name = "deptName", value = "部门名称", paramType = "query")
    })
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @Override
    public R list(Dept dept) {
        List<Dept> depts = deptService.selectList(new EntityWrapper<>(dept));
        return depts != null ? R.ok(R.OK_200).data("depts", depts) : R.error(R.ERROR_500);
    }
}
